/*
 * main.c

 *
 *  Created on: Nov 19, 2013
 *      Author: adijoshi
 */

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <cache.h>

void exec_traces();

int main()
{
	init_cache();
	exec_traces();
	return 0;
}

void exec_traces()
{
	unsigned int operation,address;
	FILE *input_file_ptr,*output_file_ptr;
	input_file_ptr = fopen("sample.txt","r");

	output_file_ptr = fopen("sample_out.txt","a");
	fprintf(output_file_ptr,"Welcome ----- here it starts! -----\n");
	fflush(output_file_ptr);

	if( input_file_ptr == NULL )
	{
		perror("Error while opening the file.\n");
	    exit(EXIT_FAILURE);
	}
	//int count=0;
	while(fscanf(input_file_ptr,"%d %x\n",&operation,&address)!=EOF)
	{
		// piece below - to test writing of sample_out.txt - done successfully.
		/*fprintf(pof,"line no %d contains -\n",count);
		fflush(pof);
		fprintf(pof,"%d %x\n",n,Addr);
		fflush(pof);
		count++;*/

		switch(operation){
		case CPU_READ_D:
		case CPU_READ_I:
			exec_Read_Req(CPU_READ_D,address);
			break;
		case CPU_WRITE_D:
			exec_Write_Req(CPU_WRITE_D,address);
			break;
		case SNOOPED_INVALIDATE:
			put_Snoop_Result('I',address);
			break;
		case SNOOPED_READ:
			put_Snoop_Result('R',address);
			break;
		case SNOOPED_WRITE:
			put_Snoop_Result('W',address);
			break;
		case SNOOPED_RFO:
			put_Snoop_Result('M',address);
			break;
		case CLEAR_CACHE:
			clear_Cache();
			break;
		case PRINT_CACHE:
			print_Cache();
			break;
		default:
			break;
		}
	}

	fclose(input_file_ptr);
	fclose(output_file_ptr);
}
